//
//  visitorLoginView.swift
//  SinaWeibo
//
//  Created by mac on 16/2/16.
//  Copyright © 2016年 mac. All rights reserved.

//访客visitor

import UIKit
import SnapKit
//定义协议  oc中@protocol  xxx<NSObject>
//swift  protocol  xxx :NSObjectProtocol  继承自根协议
@objc protocol VisitorLoginViewDelegate:NSObjectProtocol{
    //声明  可选择optional
    //登录协议
   optional func userWillLogin()
    //注册协议方法
    func userWillRegister()
}



class visitorLoginView: UIView {
    //声明代理对象 weak防止循环   
    weak var visitorLoginViewDelegate:VisitorLoginViewDelegate?
    //添加点击事件
    
    //自定义访客视图 默认构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc private func loginBtnDidClick(){
        //使用代理调用协议方法   要做两重判断 1.代理对象不为空 2.代理对象是否响应协议方法
        visitorLoginViewDelegate?.userWillLogin?()
        
    }
    
    @objc private func registerBtnDidClick(){
        //使用代理调用协议方法   要做两重判断 1.代理对象不为空 2.代理对象是否响应协议方法
        visitorLoginViewDelegate?.userWillRegister()
        
    }

    
    func setupInfoWtih(tipText: String,imageName: String?) {
        tipLabel.text = tipText
        if imageName != nil {
            //一定不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏大房子
            largeHouse.hidden = true
            //将circleIcon移到顶端
            self.bringSubviewToFront(circleIcon)

        } else {
            //一定是在首页
            startAnimation()
        }
    }
    //设置圆形图片动画效果
   private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        animation.repeatCount = MAXFLOAT
        //转一圈
        animation.toValue = 2 * M_PI
        //一圈所用时间
        animation.duration = 10
        //切换视图时也在转
        animation.removedOnCompletion = false
        //将基础动画添加到图层
        circleIcon.layer.addAnimation(animation, forKey: nil)
        
    }
    
    //自动添加的方法  重写了init方法就会添加
    private func setupUI(){
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //添加约束
        //圆形图片
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //后面的小房子
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        //背景图片
        backImage.snp_makeConstraints { (make) -> Void in
            //            make.top.equalTo(self.snp_stop)
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //文字的约束
        //        tipLabel.snp_m
        
        //图片下面的文字
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            //文字宽度
            make.width.equalTo(230)
            //文字高度
            make.height.equalTo(40)
        }
        //登录按钮约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        //注册按钮约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        self.backgroundColor = UIColor(red: 236.0/255.0, green: 236.0/255.0, blue: 236.0/255.0, alpha: 1)
        
//点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //圆形的图片   circle圆
    //懒加载图片
    
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var backImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var largeHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //    private lazy var loginBtn:UIButton = UIButton(back)
    
    
    //提示登录
    private lazy var tipLabel: UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())

        //设置按钮
    

    private lazy var loginBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录123", textColor: UIColor.darkGrayColor())
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: themeColor)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}